//
//  LXAlertView.m
//  LXAlertview
//
//  Created by zhongzhi on 2017/6/22.
//  Copyright © 2017年 zhongzhi. All rights reserved.
//

#import "LXAlertView.h"
#import "FPSevicePhonePopView.h"
#import "FPDetainmentPopupView.h"
#import "FPProtocolPopupView.h"
#import "FPNoticePopupView.h"

typedef void(^alertResult) (NSInteger index);

@interface LXAlertView ()

@property(nonatomic,copy)alertResult alertResult;
@property (nonatomic , assign) NSInteger initIndex;
@property (nonatomic , strong) NSString *phoneNumber;
@property (nonatomic , strong) NSString *detainmentTitleString;
@property (nonatomic , strong) NSString *detainmentContentString;
@property (nonatomic , strong) NSString *detainmentBtn1String;
@property (nonatomic , strong) NSString *detainmentBtn2String;

@property (nonatomic , strong) NSString *noticeTitleString;
@property (nonatomic , strong) NSString *noticeContentString;
@property (nonatomic , strong) NSString *noticeBtn1String;
@property (nonatomic , strong) NSString *noticeBtn2String;
@property (nonatomic , strong) NSString *noticeBtn3String;
@property (nonatomic , assign) NSInteger showBtnCounts;

@property (nonatomic , strong) FPSevicePhonePopView *servicePhonePopView; // 我的 -> 联系我们
@property (nonatomic , strong) FPDetainmentPopupView *detainmentPopView; // 挽留弹窗
@property (nonatomic , strong) FPNoticePopupView *noticePopView; // 首页弹窗

@end


@implementation LXAlertView

// 客服电话
- (instancetype)initContactUsServicePhone:(NSString *)phoneNumber alertAlertResult:(void (^)(NSInteger))alertResult {
    
    self = [super init];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
        
        self.alertResult = alertResult;
        self.phoneNumber = phoneNumber;
        
        self.initIndex = 1;
        [self setupWithType:1];
    }
    return self;
}


// 挽留弹窗
- (instancetype)initDetainmentWithTitleString:(NSString *)titleString contentString:(NSString *)contentString btn1:(NSString *)btn1 btn2:(NSString *)btn2 alertAlertResult:(void (^)(NSInteger index))alertResult {
    
    self = [super init];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
        
        self.alertResult = alertResult;
        self.detainmentTitleString = titleString;
        self.detainmentContentString = contentString;
        self.detainmentBtn1String = btn1;
        self.detainmentBtn2String = btn2;
        
        self.initIndex = 2;
        [self setupWithType:2];
    }
    return self;
}

// 首页弹窗
- (instancetype)initNoticeAlertWithTitleString:(NSString *)titleString contentString:(NSString *)contentString showBtnCounts:(NSUInteger)btnCounts btn1:(NSString *)btn1 btn2:(NSString *)btn2 btn3:(NSString *)btn3 alertAlertResult:(void (^)(NSInteger))alertResult {
    
    self = [super init];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
        
        self.alertResult = alertResult;
        self.noticeTitleString = titleString;
        self.noticeContentString = contentString;
        self.noticeBtn1String = btn1;
        self.noticeBtn2String = btn2;
        self.noticeBtn3String = btn3;
        self.showBtnCounts = btnCounts;
        
        self.initIndex = 3;
        [self setupWithType:3];
    }
    return self;
}



- (void)dismissAlert {
    
    [self dismiss];
}

- (void)showLXAlertView {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].windows.firstObject;
//    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:self];
    
    [self creatShowAnimation];
}

- (void)setupWithType:(NSInteger)type {
    
    // 我的 -> 联系我们
    if (type == 1) {
        
        [self addSubview:self.servicePhonePopView];
        
        self.servicePhonePopView.phoneNumber = self.phoneNumber;
        
        self.servicePhonePopView.leftBtn.tag = 1;
        
        self.servicePhonePopView.rightBtn.tag = 2;
        
        [self.servicePhonePopView.leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.servicePhonePopView.rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    // 挽留弹窗
    else if (type == 2) {
        
        
        [self addSubview:self.detainmentPopView];
        
        self.detainmentPopView.detainmentTitleString = self.detainmentTitleString;
        self.detainmentPopView.detainmentContentString = self.detainmentContentString;
        self.detainmentPopView.detainmentBtn1String = self.detainmentBtn1String;
        self.detainmentPopView.detainmentBtn2String = self.detainmentBtn2String;
        
        self.detainmentPopView.btn1.tag = 1;
        
        self.detainmentPopView.btn2.tag = 2;
        
        [self.detainmentPopView.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.detainmentPopView.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    // 首页弹窗
    else if (type == 3) {
        
        [self addSubview:self.noticePopView];
        
        self.noticePopView.mainTitleString = self.noticeTitleString;
        self.noticePopView.contentString = self.noticeContentString;
        self.noticePopView.btn1String = self.noticeBtn1String;
        self.noticePopView.btn2String = self.noticeBtn2String;
        self.noticePopView.btn3String = self.noticeBtn3String;
        
        [self.noticePopView showBtnCounts:self.showBtnCounts];
        
        self.noticePopView.btn1.tag = 1;
        
        self.noticePopView.btn2.tag = 2;
        
        self.noticePopView.btn3.tag = 3;
        
        [self.noticePopView.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.noticePopView.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.noticePopView.btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}




- (void)btnClick:(UIButton *)button {
    
    if (self.alertResult) {
            
        [self dismiss];
                
        self.alertResult(button.tag);
    }
    [self removeFromSuperview];
}




- (void)creatShowAnimation {
    
    if (self.initIndex == 1) {
        
        self.servicePhonePopView.transform = CGAffineTransformMakeScale(0.90, 0.90);
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.servicePhonePopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    else if (self.initIndex == 2) {
        
        self.detainmentPopView.transform = CGAffineTransformMakeScale(0.90, 0.90);
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.detainmentPopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    else if (self.initIndex == 3) {
        
        self.noticePopView.transform = CGAffineTransformMakeScale(0.90, 0.90);
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.noticePopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)dismiss {
    
    if (self.initIndex == 1) {
        
        self.servicePhonePopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        
        [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.servicePhonePopView.transform = CGAffineTransformMakeScale(0.0, 0.0);
        } completion:^(BOOL finished) {
            
            [self removeFromSuperview];
        }];
    }
    else if (self.initIndex == 2) {
        
        self.detainmentPopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        
        [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.detainmentPopView.transform = CGAffineTransformMakeScale(0.0, 0.0);
        } completion:^(BOOL finished) {
            
            [self removeFromSuperview];
        }];
    }
    else if (self.initIndex == 3) {
        
        self.noticePopView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        
        [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            
            self.noticePopView.transform = CGAffineTransformMakeScale(0.0, 0.0);
        } completion:^(BOOL finished) {
            
            [self removeFromSuperview];
        }];
    }
}



#pragma mark - lazy loads
- (FPSevicePhonePopView *)servicePhonePopView {
    
    if (!_servicePhonePopView) {
        
        _servicePhonePopView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPSevicePhonePopView.class) owner:self options:nil].firstObject;
        
        _servicePhonePopView.frame = CGRectMake((SCREEN_WIDTH - 302.f) / 2.f, (SCREEN_HEIGHT -UIDevice.safeDistanceBottom - 160) / 2.f, 302.f, 195.f);
    }
    
    return _servicePhonePopView;
}

- (FPDetainmentPopupView *)detainmentPopView {
    
    if (!_detainmentPopView) {
        
        _detainmentPopView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPDetainmentPopupView.class) owner:self options:nil].firstObject;
        
        _detainmentPopView.frame = CGRectMake((SCREEN_WIDTH - 302.f) / 2.f, (SCREEN_HEIGHT -UIDevice.safeDistanceBottom - 160) / 2.f - 100.f , 302.f, 280.f);
    }
    
    return _detainmentPopView;
}

- (FPNoticePopupView *)noticePopView {
    
    if (!_noticePopView) {
        
        _noticePopView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPNoticePopupView.class) owner:self options:nil].firstObject;
        
        CGFloat width = SCREEN_WIDTH - 32.f;
        _noticePopView.frame = CGRectMake((SCREEN_WIDTH - width) / 2.f, (SCREEN_HEIGHT -UIDevice.safeDistanceBottom - 160) / 2.f - 100.f , width , 280.f);
    }
    
    return _noticePopView;
}


@end
